﻿#include "Mesh.h"

#include "libmesh/elem.h"
#include "libmesh/mesh_base.h"
#include <vtkm/cont/DataSetBuilderExplicit.h>

namespace poisson
{
void Mesh::prepare_for_use()
{
  std::vector<vtkm::UInt8> shapes;
  std::vector<IdComponent> numindices;
  std::vector<Id> conn;
  for (const auto& elem : active_local_element_ptr_range())
  {
    auto type = elem->type();
    if (type == libMesh::ElemType::QUAD4)
    {
      shapes.push_back(vtkm::CellShapeIdEnum::CELL_SHAPE_QUAD);
    }
    if (type == libMesh::ElemType::TRI3)
    {
      shapes.push_back(vtkm::CellShapeIdEnum::CELL_SHAPE_TRIANGLE);
    }

    numindices.push_back(elem->n_nodes());
    for (size_t i = 0; i < elem->n_nodes(); i++)
    {
      conn.push_back(elem->node_id(i));
    }
  }

  using CoordType = vtkm::Vec3f;
  std::vector<CoordType> coords;
  for (const auto& node : local_node_ptr_range())
  {
    coords.emplace_back((*node)(0), (*node)(1), 0);
  }
  vtkm::cont::DataSetBuilderExplicit dsb;
  this->_data_set = dsb.Create(coords, shapes, numindices, conn, "coordinates");

  libMesh::Mesh::prepare_for_use();
}
}
